package com.atguigu.springbootdemo.customer.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.springbootdemo.customer.bean.Customer;
import com.atguigu.springbootdemo.customer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * ClassName: CustomerController
 * Package: com.atguigu.springbootdemo.controller
 * Description:
 *
 * @Author:
 * @Create: 2024/3/12 14:15
 * @Version: 1.0
 *
 */

/*
控制层组件（类）
 * 职责：
 *  1. 接收客户端的请求和请求参数
 *  2. 调用服务层
 *  3. 给客户端相应结果
 *
 * @RestController 将类标识为控制层组件
 * Spring框架提供了IOC容器 会将标注了注解的类的对象 管理到容器中
 */
@RestController  // 等价于controller+ responseBody
public class CustomerController {

    // 自动装配 得到service层对象
    @Qualifier("csi")
    @Autowired
    CustomerService customerService;

    /*
    请求处理方法：
    客户端请求：http://localhost:8080/sayHello
     */
    @RequestMapping(value = "/sayHello")
    public String sayHello() {
        System.out.println("CustomerController.sayHello");
        return "success";
    }

    /*
    @RequestParam 将请求中的参数映射到请求处理方法的形参上
    @PathVariable
    @RequestBody 要求请求参数名字和对象属性名一致
     请求参数：
    1. 键值对
    2. 路径拼接
    3. 请求体
     */
    @RequestMapping(value = "/param1")
    public String param1(@RequestParam(value="username") String username,
                         @RequestParam(value="age")Integer age) {
        System.out.println("param1:" + username + age);
        return "success";
    }

    @RequestMapping(value="/param2/{username}/{age}")
    public String param2(@PathVariable(value="username") String username, @PathVariable(value="age")Integer age) {

        System.out.println("param2:" + username + age);
        return "success";
    }

    @RequestMapping(value="/param3")
    public String param3(@RequestBody Customer customer) {
        System.out.println("param3:" + customer);
        return "success";
    }

    /*
    响应数据格式: json
     */
    @RequestMapping(value="resulttype")
    public String resultType() {
       // return "success";
        Customer customer = new Customer(1001, "zhangsan",20);

        // return customer; // springmvc 会将Java对象通过Jackson技术转换成json字符串
        return JSON.toJSONString(customer);
    }

    /*
    请求方式：
    get:读
    post:写
     */

    // @RequestMapping(value="/requestmethod", method = RequestMethod.GET)
    // @GetMapping(value = "/requestmethod")
    @GetMapping("/requestmethod")
    // @PostMapping(value = "/requestmethod")
    public String requestMethod() {
        return "success";
    }

    /*
    状态码
    200：OK
    400：请求参数错误
    404：请求资源不存在
    405：请求方式不正确
    500：服务端报错

    动态参数
     */
    @RequestMapping(value = "/statuscode", method = RequestMethod.GET)
    public String statusCode(@RequestParam(value="username", required = false, defaultValue = "unknown") String username,
                             @RequestParam(value="age", required = false, defaultValue = "18")Integer age) {
        System.out.println("statusCode:" + username + age);
        return "success";
    }

    // crud-------------------------------------------------------------------------------------
    @GetMapping(value="/getcustomer/{id}")
    public String getCustomer(@PathVariable(value="id") Integer id) {
        // 调用服务层
        //自己定义的方法
//         Customer customer = customerService.getCustomer(id);

        //MP的方法
        Customer customer = customerService.getById(id);

        return JSON.toJSONString(customer);
    }

    @GetMapping(value="/getcustomerlist")
    public List<Customer> getCustomerList() {
        List<Customer> customerList = customerService.getCustomerList();
//        List<Customer> customerList = customerService.list();
        return customerList;
    }

    @PostMapping(value="/savecustomer")
    public String savaCustomer(@RequestBody Customer customer) {
        // 调用服务层 自己写的方法
//        customerService.savaCustomer(customer);
        customerService.save(customer);
        return "success";
    }

    @PostMapping(value="/updatecustomer")
    public String updateCustomer(@RequestBody Customer customer) {
        // 调用服务层自己写的方法
//        customerService.updateCustomer(customer);
        customerService.updateById(customer);
        return "success";
    }

    @PostMapping(value="/saveorupdatecustomer")
    public String savaOrUpdateCustomer(@RequestBody Customer customer) {
        // mp方法 根据主键是否为空来决定 是插入还是修改
        customerService.saveOrUpdate(customer);
        return "success";
    }

    @GetMapping(value="/deletecustomer/{id}")
    public String deleteCustomer(@PathVariable("id") Integer id) {
//        customerService.deleteCustomer(id);
        customerService.removeById(id);
        return "success";
    }

    // 动态sql sql注入
    @GetMapping("/selectcustomer")
    public List<Customer> selectCustomer(@RequestParam(value="username",required = false) String username,
                                         @RequestParam(value="age", required = false) Integer age) {
        List<Customer> customers = customerService.selectCustomerByCondition(username,age);
        return customers;

    }
}
